<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="800">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item :label="t('scxInitialMajor.fields.id')">
        <div v-if="formData.majorType === 1">材料物理与化学</div>
        <div v-if="formData.majorType === 2">材料学</div>
        <div v-if="formData.majorType === 3">材料加工工程</div>
        <div v-if="formData.majorType === 4">腐蚀科学与防护</div>
      </el-form-item>
      <el-form-item :label="t('scxInitialMajor.fields.reviewTime')" prop="reviewStartTime">
        <el-date-picker
          v-model="reviewTime"
          type="datetimerange"
          value-format="x"
          :start-placeholder="
            t('common.selectTextPlaceholder') + t('scxInitialMajor.fields.reviewStartTime')
          "
          :end-placeholder="
            t('common.selectTextPlaceholder') + t('scxInitialMajor.fields.reviewEndTime')
          "
        />
      </el-form-item>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item :label="t('scxInitialMajor.fields.phd1st')" prop="phd1st">
            <el-input-number
              v-model="formData.phd1st"
              :placeholder="t('common.inputTextPlaceholder') + t('scxInitialMajor.fields.phd1st')"
              :min="0"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="t('scxInitialMajor.fields.phd2nd')" prop="phd2nd">
            <el-input-number
              v-model="formData.phd2nd"
              :placeholder="t('common.inputTextPlaceholder') + t('scxInitialMajor.fields.phd2nd')"
              :min="0"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="t('scxInitialMajor.fields.phd3rd')" prop="phd3rd">
            <el-input-number
              v-model="formData.phd3rd"
              :placeholder="t('common.inputTextPlaceholder') + t('scxInitialMajor.fields.phd3rd')"
              :min="0"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item :label="t('scxInitialMajor.fields.master1st')" prop="master1st">
            <el-input-number
              v-model="formData.master1st"
              :placeholder="
                t('common.inputTextPlaceholder') + t('scxInitialMajor.fields.master1st')
              "
              :min="0"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8" />
        <el-col :span="8" />
      </el-row>
      <el-link type="primary" @click="handleShowJudge" :disabled="!formData.id">
        {{ t('judgeScene.card.viewJudge') }}
      </el-link>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading"
        >{{ t('common.ok') }}
      </el-button>
      <el-button @click="dialogVisible = false">{{ t('common.cancel') }}</el-button>
    </template>
  </Dialog>

  <ScxInitialMajorJudgeSelect ref="scxInitialMajorJudgeSelectRef" />
</template>
<script setup lang="ts">
import { ScxInitialMajorApi, ScxInitialMajorVO } from '@/api/awards/scxinitialmajor'
import { FormRules } from 'element-plus'
import ScxInitialMajorJudgeSelect from '@/views/awards/scxinitialmajorjudge/components/ScxInitialMajorJudgeSelect.vue'

/** 师昌绪初评-专业分组 表单 */
defineOptions({ name: 'ScxInitialMajorForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  awardId: undefined,
  majorType: undefined,
  reviewStartTime: undefined,
  reviewEndTime: undefined,
  phd1st: undefined,
  phd2nd: undefined,
  phd3rd: undefined,
  master1st: undefined,
  remark: undefined
})
const formRules = reactive<FormRules>({
  reviewStartTime: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('scxInitialMajor.fields.reviewStartTime'),
      trigger: 'blur'
    }
  ],
  reviewEndTime: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('scxInitialMajor.fields.reviewEndTime'),
      trigger: 'blur'
    }
  ],
  phd1st: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('scxInitialMajor.fields.phd1st'),
      trigger: 'blur'
    }
  ],
  phd2nd: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('scxInitialMajor.fields.phd2nd'),
      trigger: 'blur'
    }
  ],
  phd3rd: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('scxInitialMajor.fields.phd3rd'),
      trigger: 'blur'
    }
  ],
  master1st: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('scxInitialMajor.fields.master1st'),
      trigger: 'blur'
    }
  ]
})
const formRef = ref() // 表单 Ref

const reviewTime = computed({
  get() {
    return [formData.value.reviewStartTime, formData.value.reviewEndTime]
  },
  set(value) {
    formData.value.reviewStartTime = value[0] ?? undefined
    formData.value.reviewEndTime = value[1] ?? undefined
  }
})

/** 打开弹窗 */
const open = async (awardsId: string, type: number, major?: ScxInitialMajorVO) => {
  dialogVisible.value = true
  if (major?.id) {
    dialogTitle.value = t('action.update')
    formType.value = 'update'
  } else {
    dialogTitle.value = t('action.create')
    formType.value = 'create'
  }
  resetForm()
  // 修改时，设置数据
  if (major?.id) {
    formLoading.value = true
    try {
      formData.value = await ScxInitialMajorApi.getScxInitialMajor(major.id)
    } finally {
      formLoading.value = false
    }
  }
  formData.value.awardId = awardsId as any
  formData.value.majorType = type as any
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ScxInitialMajorVO
    if (formType.value === 'create') {
      await ScxInitialMajorApi.createScxInitialMajor(data)
      message.success(t('common.createSuccess'))
    } else {
      await ScxInitialMajorApi.updateScxInitialMajor(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    awardId: undefined,
    majorType: undefined,
    reviewStartTime: undefined,
    reviewEndTime: undefined,
    phd1st: undefined,
    phd2nd: undefined,
    phd3rd: undefined,
    master1st: undefined,
    remark: undefined
  }
  formRef.value?.resetFields()
}

const scxInitialMajorJudgeSelectRef = ref<InstanceType<typeof ScxInitialMajorJudgeSelect>>()
const handleShowJudge = () => {
  scxInitialMajorJudgeSelectRef.value?.open(formData.value.awardId, formData.value.id)
}
</script>
